
import instance from "../../url/chat.js";

const chat_createRoom = async (user_id,created_user=0,type='group') => {
    // user_id=[]/int,created_user=创建者
    // 实际只有群聊用这个api
    console.log('创建房间',user_id,created_user,type)
    return await instance.post('/ws/set/room/create', {
        user_id: user_id,
        created_user: created_user,
        type:type
    })
        .then((response) => { 
            console.log(response)
            return response
        })
}

const add_user_to_chat_room = async (dataform) => {
    // user_id=[],room_id=str,type=str
    return await instance.post('/ws/set/room/add_user', dataform)
        .then((response) => { 
                
        })
}

const  get_user_GroupList=async (user_id) => {
    // room_id=str
    return await instance.get(`/ws/get/group_list?user_id=${user_id}`)
        .then((response) => { 
         
            if(response==0){
                return []
            }else{
                return response
            }
        })
}

const get_group_UserInfo=async (room_id,limit=1) => {
    // roomid=str  limit=int
    return await instance.get(`/ws/get/group/user_info?room_id=${room_id}&limit=${limit}`)
        .then((response) => { 

        })
}


const get_user_FriendList=async (user_id) => {
    // room_id=str
    return await instance.get(`/ws/get/friends_list?user_id=${user_id}`)
        .then((response) => { 
           if(response==0){
                return []
            }else{
                return response
            }
        })
}


const flush_room_stream=async (room_id,user_id,stream_time,type=1) => {
    // room_id=str
    return await instance.post(`/ws/flush/room_stream?user_id=${user_id}&type=${type}&stream_time=${stream_time}`, {
        room_id:room_id,
    })
        .then((response) => { 
            return response
        })
}
const search_user_friend_group=async (user_id,key,limit=1) => {
    // room_id=str
    return await instance.post(`/ws/user/user_group/search?user_id=${user_id}&key=${key}&limit=${limit}`)
        .then((response) => { 
            if (response == 0) {
                return []
            } else { 
                return response
            }
        })
}

const find_user_group=async (user_id,key,limit=1) => {
    // room_id=str
    return await instance.post(`/ws/user/search/group_user?key=${key}&user_id=${user_id}&limit=${limit}`)
        .then((response) => { 
           
            if (response == 0) {
                return []
            } else { 
                
                return response
            }
        })
}

const get_room_info_by_room_id_type= async (room_ids,type,user_id=0) => {
    //该api用于处理好友的房间信息
    return await instance.post(`/ws/room/get_info/by/room_id_type?type=${type}&user_id=${user_id}`,
        room_ids
    )
        .then((response) => {
            if (response== 0) { 
                return []
            } 
            return response

        })  
}

const get_room_info_all_by_room_id_type=async (room_id,type,user_id) => {
    //该api用于处理好友的房间信息
    return await instance.post(`/ws/room/get_info/all/by/room_id_type?type=${type}&room_id=${room_id}&user_id=${user_id}`,
    )
        .then((response) => {
            if (response == 0) {  
                return {}
            }
            return response

        })  
}


const add_frined=async (user_id,friend_id) => {
    if(user_id==undefined || friend_id==undefined)return
    return await instance.post(`/ws/user/friend/add?user_id=${user_id}&friend_id=${friend_id}`)
        .then((response) => { 
            return response
        })
}
const add_group = async (user_id, room_id) => { 
    return await instance.post(`/ws/user/group/add?user_id=${user_id}&room_id=${room_id}`)
        .then((response) => { 
                return response
        })
}

const delete_friends = async (user_id, friend_id) => { 
    return await instance.post(`/ws/user/friends/delete?user_id=${user_id}&friend_id=${friend_id}`)
            .then((response) => { 
                    return response
            })
}
const quit_group = async (user_id, room_id) => { 
    return await instance.post(`/ws/user/group/quit?user_id=${user_id}&room_id=${room_id}`)
            .then((response) => { 
                return response
            })
}
const get_user_room_id_by_user_ids = async (user_id) => { 
    return await instance.get(`/ws/room/get_ids/all?user_id=${user_id}`)
        .then((response) => { 
                return response
            })
}



export { 
    chat_createRoom,
    add_user_to_chat_room,
    get_group_UserInfo,
    get_user_GroupList, get_user_FriendList,
    add_frined, 
    flush_room_stream,
    search_user_friend_group, find_user_group,
    get_room_info_by_room_id_type, get_room_info_all_by_room_id_type,
    add_group,delete_friends,quit_group,get_user_room_id_by_user_ids

}